package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Order;
import cn.wolfcode.trip.base.query.OrderQueryObject;
import cn.wolfcode.trip.base.service.IOrderService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    /**
     * 查找所有订单内容
     */
    @GetMapping
    public PageInfo listByState(OrderQueryObject qo) {
        return orderService.query(qo);
    }

    /**
     * 根据用户id查询订单对应明细
     */
    @GetMapping("/{userId}")
    public Order selectById(@PathVariable Long id) {
        return orderService.selectByPrimaryKey(id);
    }

    /**
     * 添加购物车订单
     *
     * @param order
     * @return
     */
    @PostMapping("/{product.id}")
    public JsonResult saveShopCar(Order order) {
        JsonResult result = new JsonResult();
        try {
            orderService.insert(order);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作异常");
        }
        return result;
    }

    /**
     * 添加购买订单
     *
     * @param order
     * @return
     */
    @PostMapping("/2")
    public JsonResult savePay(Order order) {
        JsonResult result = new JsonResult();
        try {
            orderService.insertPay(order);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作异常");
        }
        return result;
    }

    /**
     * 查找购物车订单内容
     */
    @GetMapping("/car")
    public PageInfo listByStateAndUserId(OrderQueryObject qo) {
        qo.setPageSize(0);
        return orderService.list(qo);
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Long id) {
        JsonResult result = new JsonResult();
        try {
            orderService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作异常");
        }
        return result;
    }


    /**
     * 改变订单的状态
     * @param order
     * @return
     */
    @PostMapping
    public JsonResult updata(Order order) {
        JsonResult result = new JsonResult();
        try {
            orderService.updateByPrimaryKey(order);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作异常");
        }
        return result;
    }
}
